/*
* Copyright 2004-2010 mb_sx@hotmail.com
* All Rights Reserved.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace OwnControls
{
    public partial class DataStorageControl : UserControl
    {
        private string folderName;
        
        public string DataFileName
        {
            get { return txtDataFile.Text; }
            set { this.txtDataFile.Text = value; }
        }


        public string ArchiveFileName
        {
            get { return this.txtArchiveFile.Text; }
            set { this.txtArchiveFile.Text = value; }
        }

        private bool openDirectory = false;
        /// <summary>
        /// Tells DataStorageControl that provided paths are directories not full filenames.
        /// </summary>
        public bool OpenDirectory
        {
            set { openDirectory = value; }
            get { return openDirectory; }
        }

        public bool CheckFileExists
        {
            get { return openFile.CheckFileExists; }
            set { openFile.CheckFileExists = value; }
        }
        
        public DataStorageControl()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog(txtDataFile, OwnControls.Resources.OPEN_ARCHIVE);
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            OpenFileDialog(txtArchiveFile, OwnControls.Resources.OPEN_ARCHIVE);
        }

        /// <summary>
        /// Shows file open dialog for particular file 
        /// </summary>
        /// <param name="file">TextBox where filename is taken and updated</param>
        /// <param name="title">Title for file open dialog</param>
        /// <returns></returns>
        private bool OpenFileDialog(TextBox file, string title)
        {
            openFile.Title = title;
            if (file.Text == "")
            {
                if (folderName == "")
                    openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                else
                    openFile.InitialDirectory = folderName;
            }
            else
            {
                FileInfo f = new FileInfo(file.Text);
                if (openDirectory)
                {
                    openFile.InitialDirectory = f.FullName;
                }
                else
                {
                    openFile.InitialDirectory = f.DirectoryName;
                    openFile.FileName = f.Name;
                }
            }
            if (DialogResult.OK == openFile.ShowDialog())
            {
                file.Text = openFile.FileName;
                FileInfo fd = new FileInfo(openFile.FileName);
                folderName = fd.DirectoryName;
             
                return true;
            }
            return false;
        }
        
    }
}
